package com.diet263.activity;

import java.util.ArrayList;
import java.util.List;

import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.diet263.nfcleedemo.R;
import com.diet263.utils.MyLog;
import com.diet263.utils.WifiAccessPointAdapetr;
import com.diet263.utils.WifiAdmin;
import com.diet263.view.WriteWifiConfigToNfcDialog;

public class WriteWifiConnectionActivity extends BaseActivity implements OnItemClickListener, OnClickListener {
	private static final String TAG = "WriteWifiConnectionActivity";
	private WifiAdmin wifiAdmin;
	private ListView lvWifiList;
	private List<ScanResult> resultList = new ArrayList<ScanResult>();
	private List<WifiConfiguration> configList = new ArrayList<WifiConfiguration>();
	private WifiAccessPointAdapetr mApAdper;
	private TextView tvActionNote;
	private TextView tvWifiStatus;
	private TextView tvScanWifi;
	private TextView tvOpenCloseWifi;
	private int mWifiState = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_write_wifi_connection);
		initView();
		setViewStatus();
	}

	private void initView() {
		lvWifiList = (ListView) findViewById(R.id.lv_wifi_list);
		tvActionNote = (TextView) findViewById(R.id.tv_action_note);
		tvWifiStatus = (TextView) findViewById(R.id.tv_wifi_status);
		tvScanWifi = (TextView) findViewById(R.id.tv_scan_wifi);
		tvOpenCloseWifi = (TextView) findViewById(R.id.tv_open_wifi);
		lvWifiList.setOnItemClickListener(this);
		tvScanWifi.setOnClickListener(this);
		tvOpenCloseWifi.setOnClickListener(this);
		wifiAdmin = new WifiAdmin(this);
		mApAdper = new WifiAccessPointAdapetr(this, resultList, configList);
		lvWifiList.setAdapter(mApAdper);
	}

	private void setViewStatus() {
		showWifiStatus();
	}

	private void showWifiStatus() {
		mWifiState = wifiAdmin.checkState();
		if (WifiManager.WIFI_STATE_DISABLED == mWifiState) {
			tvWifiStatus.setText("wifi:已关闭");
			tvActionNote.setText("点击底部按钮开启wifi");
		} else if (WifiManager.WIFI_STATE_ENABLED == mWifiState) {
			tvWifiStatus.setText("wifi:已开启");
			tvActionNote.setText("点击列表中要写入的wifi热点");
		} else if (WifiManager.WIFI_STATE_DISABLED == mWifiState) {
			tvWifiStatus.setText("wifi:正在开启");
			tvActionNote.setText("扫描wifi热点...");
		}
	}

	private void showWifiList() {
		if (WifiManager.WIFI_STATE_DISABLED == mWifiState) {
			return;
		} else if (WifiManager.WIFI_STATE_ENABLED == mWifiState) {
			wifiAdmin.startScan();
			getWifiList();
		}
	}

	private void getWifiList() {

		showWifiConfigurationInfo();
		showWifiListInfo();
		if (!resultList.isEmpty()) {
			mApAdper.notifyDataSetChanged();
		}
	}

	private void showWifiConfigurationInfo() {
		for (int i = 0; i < wifiAdmin.getConfiguration().size(); i++) {
			MyLog.i(TAG, "Configuration " + wifiAdmin.getConfiguration().get(i).SSID);
		}
		if (wifiAdmin.getConfiguration().size() > 0) {
			configList.clear();
			for (int i = 0; i < wifiAdmin.getConfiguration().size(); i++) {
				if (configList.isEmpty()) {
					configList.add(wifiAdmin.getConfiguration().get(i));
				} else {
					boolean isNoHas = true;
					for (int j = 0; j < configList.size(); j++) {
						if (configList.get(j).SSID.equals(wifiAdmin.getConfiguration().get(i).SSID)) {
							isNoHas = false;
							break;
						}
					}
					if (isNoHas) {
						configList.add(wifiAdmin.getConfiguration().get(i));
					}
				}
			}
		}
	}

	private void showWifiListInfo() {
		for (int i = 0; i < wifiAdmin.getWifiList().size(); i++) {
			MyLog.i(TAG, "ScanResult " + wifiAdmin.getWifiList().get(i).SSID);
		}
		if (wifiAdmin.getWifiList().size() > 0) {
			resultList.clear();
			for (int i = 0; i < wifiAdmin.getWifiList().size(); i++) {
				if (resultList.isEmpty()) {
					resultList.add(wifiAdmin.getWifiList().get(i));
				} else {
					boolean isNoHas = true;
					for (int j = 0; j < resultList.size(); j++) {
						if (resultList.get(j).SSID.equals(wifiAdmin.getWifiList().get(i).SSID)) {
							isNoHas = false;
							break;
						}
					}
					if (isNoHas) {
						resultList.add(wifiAdmin.getWifiList().get(i));
					}
				}
			}
		}
	}

	public void onResume() {
		super.onResume();
		showWifiList();
	}

	public void onPause() {
		super.onPause();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		distributeClickEvent(position);
	}

	private void distributeClickEvent(int position) {
		ScanResult scanResult = resultList.get(position);
		WifiConfiguration wifiConfiguration = null;
		for (WifiConfiguration config : configList) {
			if (config.SSID.replace("\"", "").equals(scanResult.SSID)) {
				wifiConfiguration = config;
				break;
			}
		}
		if (wifiConfiguration != null) {
			new WriteWifiConfigToNfcDialog(this, wifiConfiguration, wifiAdmin.getWifiManager()).show();
		} else {

		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_scan_wifi:

			break;
		case R.id.tv_open_wifi:

			break;

		default:
			break;
		}

	}

}
